﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMS.HelperClasses;
using WHMS.HelperClasses;
using Microsoft.Reporting.WinForms;
using SD.LLBLGen.Pro.ORMSupportClasses;
using WHMS.EntityClasses;

namespace WMS.Reports
{	
	public partial class DetailedOutputReportForAllItemsForm : Form
	{

        public int selectedStoreID;

		public DetailedOutputReportForAllItemsForm()
		{
			InitializeComponent();
		}

		private void ItemBalanceForm_Load(object sender, EventArgs e)
		{
			//FormsHelper.LoadComboBox(StoresDDL, StoreFields.Id, StoreFields.Name, (x, y) => DALHelper.GetDynamicList(x, y, null));
			FormsHelper.LoadComboBox(ProjectsDDL, ProjectFields.Id, ProjectFields.Name, (x, y) => DALHelper.GetDynamicList(x, y, null));

			//this.reportViewer1.RefreshReport();
			new FormsHelper().DisableReportExportToWord(this.reportViewer1);
		}

		private void RunBtn_Click(object sender, EventArgs e)
		{
            var contractorAccountingNumber = (string)DALHelper.GetValue(ContractorFields.AccountingNumber, ContractorFields.Id, FormsHelper.GetSelectedId(ContractorsDDL));

			bindingSource1.DataSource = Business.GetDetailedOutput(FormsHelper.GetSelectedId(ProjectsDDL), FormsHelper.GetSelectedId(ContractorsDDL),null, selectedStoreID, FromDateDTP.Value, ToDateDTP.Value);

			this.reportViewer1.LocalReport.DataSources.Clear();
			this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", bindingSource1));
			this.reportViewer1.LocalReport.SetParameters(new ReportParameter("StoreName",lblStoreName.Text));
			//this.reportViewer1.LocalReport.SetParameters(new ReportParameter("ItemName", FormsHelper.GetSelectedName(ItemsDDL).Replace('(', ' ').Replace(')', ' ')));
			this.reportViewer1.LocalReport.SetParameters(new ReportParameter("ProjectName", FormsHelper.GetSelectedName(ProjectsDDL)));
			this.reportViewer1.LocalReport.SetParameters(new ReportParameter("ContractorName", FormsHelper.GetSelectedName(ContractorsDDL)));
			this.reportViewer1.LocalReport.SetParameters(new ReportParameter("FromDate", FromDateDTP.Value.ToShortDateString()));
			this.reportViewer1.LocalReport.SetParameters(new ReportParameter("ToDate", ToDateDTP.Value.ToShortDateString()));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("ContractorAccountingNumber", contractorAccountingNumber));

		    ProjectEntity proj = Business.GetProjectById(
		        int.Parse(
		            ProjectsDDL.SelectedValue.ToString()));

		    this.reportViewer1.LocalReport.SetParameters(new ReportParameter("ProjectCode",
		                                                                     proj != null ? proj.Code : string.Empty));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("StoreCode", txtStoreCode.Text));
			this.reportViewer1.RefreshReport();   

		}

		private void ProjectsDDL_SelectedIndexChanged(object sender, EventArgs e)
		{
			var projectId = FormsHelper.GetSelectedId(ProjectsDDL);

			var itemsFilter = new RelationPredicateBucket();
			itemsFilter.PredicateExpression.Add(new FieldCompareSetPredicate(ItemViewFields.Id, null, ProjectItemPlanFields.ItemId, null, SetOperator.In, new PredicateExpression(ProjectItemPlanFields.ProjectId == projectId)));
			//FormsHelper.LoadComboBox(ItemsDDL, ItemViewFields.Id, ItemViewFields.NameCodeUnit, () => Business.GetItemsView(itemsFilter));

			var contractorsFilter = new RelationPredicateBucket();
			contractorsFilter.PredicateExpression.Add(new FieldCompareSetPredicate(ContractorFields.Id, null, ProjectContractorFields.ContractorId, null, SetOperator.In, new PredicateExpression(ProjectContractorFields.ProjectId == projectId)));
			FormsHelper.LoadComboBox(ContractorsDDL, ContractorFields.Id, ContractorFields.Name, (x, y) => DALHelper.GetDynamicList(x, y, contractorsFilter));

			//RunBtn.Enabled = (ItemsDDL.Items.Count > 0 && ContractorsDDL.Items.Count > 0);
		}

        private void btnSearchStore_Click(object sender, EventArgs e)
        {
            var searchForm = new fromGeneralEntityPicker("Store");
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtStoreCode.Text = searchForm.SelectedItemCode;

            }
        }


        private void txtStoreCode_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtStoreCode.Text))
            {
                DataTable DTStore = Business.GetStoreDetails(txtStoreCode.Text);
                if (DTStore.Rows.Count > 0)
                {
                    selectedStoreID = Int32.Parse(DTStore.Rows[0][0].ToString());
                    lblStoreName.Text = DTStore.Rows[0][2].ToString();
                    RunBtn.Enabled = true;
                }
                else
                {
                    lblStoreName.Text = string.Empty;
                    RunBtn.Enabled = false;
                }
            }
        }
	}

	
}
